// ==================== 3. 数据解析服务 ====================
package org.chen.flight.service.flight;

import lombok.extern.slf4j.Slf4j;
import org.chen.common.flight.FlightSearchData;
import org.chen.common.flight.request.FlightSearchRequest;
import org.chen.common.flight.FlightSegment;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 航班数据解析服务
 * 负责解析API返回的原始数据
 */
@Service
@Slf4j
public class FlightDataParseService {


    /**
     * 解析搜索数据
     */
    public FlightSearchData parseSearchData(Map<String, Object> data, FlightSearchRequest request) {
        FlightSearchData searchData = new FlightSearchData();
        searchData.setTransactionID(getStringValue(data, "transactionID"));
        searchData.setAdultCount(getIntValue(data, "adultCount"));
        searchData.setChildCount(getIntValue(data, "childCount"));
        searchData.setInfantCount(getIntValue(data, "infantCount"));
        searchData.setFlightWay(getStringValue(data, "flightWay"));
        searchData.setCabin(getStringValue(data, "cabin"));
        searchData.setScope(getStringValue(data, "scope"));
        searchData.setSegmentNo(getIntValue(data, "segmentNo"));
        searchData.setDirectFlight(getBooleanValue(data, "directFlight"));
        searchData.setNoRecommend(getBooleanValue(data, "noRecommend"));

        Map<String, Object> extensionAttributes = (Map<String, Object>) data.get("extensionAttributes");
        if (extensionAttributes != null) {
            searchData.setExtensionAttributes(extensionAttributes);
        }

        List<Map<String, Object>> segments = (List<Map<String, Object>>) data.get("flightSegments");
        if (segments != null) {
            List<FlightSegment> flightSegments = new ArrayList<>();
            for (Map<String, Object> segment : segments) {
                flightSegments.add(parseFlightSegment(segment));
            }
            searchData.setFlightSegments(flightSegments);
        }

        Map<String, Object> extGlobalSwitches = (Map<String, Object>) data.get("extGlobalSwitches");
        if (extGlobalSwitches != null) {
            searchData.setExtGlobalSwitches(extGlobalSwitches);
        }

        return searchData;
    }

    private FlightSegment parseFlightSegment(Map<String, Object> segment) {
        FlightSegment flightSegment = new FlightSegment();
        flightSegment.setDepartureCityCode(getStringValue(segment, "departureCityCode"));
        flightSegment.setArrivalCityCode(getStringValue(segment, "arrivalCityCode"));
        flightSegment.setDepartureCityName(getStringValue(segment, "departureCityName"));
        flightSegment.setArrivalCityName(getStringValue(segment, "arrivalCityName"));
        flightSegment.setDepartureDate(getStringValue(segment, "departureDate"));
        flightSegment.setDepartureCountryId(getIntValue(segment, "departureCountryId"));
        flightSegment.setDepartureCountryName(getStringValue(segment, "departureCountryName"));
        flightSegment.setDepartureCountryCode(getStringValue(segment, "departureCountryCode"));
        flightSegment.setDepartureProvinceId(getIntValue(segment, "departureProvinceId"));
        flightSegment.setDepartureCityId(getIntValue(segment, "departureCityId"));
        flightSegment.setArrivalCountryId(getIntValue(segment, "arrivalCountryId"));
        flightSegment.setArrivalCountryName(getStringValue(segment, "arrivalCountryName"));
        flightSegment.setArrivalCountryCode(getStringValue(segment, "arrivalCountryCode"));
        flightSegment.setArrivalProvinceId(getIntValue(segment, "arrivalProvinceId"));
        flightSegment.setArrivalCityId(getIntValue(segment, "arrivalCityId"));
        flightSegment.setDepartureCityTimeZone(getIntValue(segment, "departureCityTimeZone"));
        flightSegment.setArrivalCityTimeZone(getIntValue(segment, "arrivalCityTimeZone"));
        flightSegment.setTimeZone(getIntValue(segment, "timeZone"));
        return flightSegment;
    }

    // 辅助方法
    private String getStringValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return value != null ? value.toString() : null;
    }

    private int getIntValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        return 0;
    }

    private boolean getBooleanValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return value instanceof Boolean ? (Boolean) value : false;
    }
}